package com.shujia.flink.core;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo1WordCount {
    public static void main(String[] args) throws Exception {

        //1、创建flink的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //修改数据缓冲的超时时间，默认时200毫秒
        //改成0了，每一条数据都会发送一次，延迟极低，但是会降低吞吐量
        //env.setBufferTimeout(0);

        //设置并行度，相当于spark中的分区数，一个并行度对应一个task
        //env.setParallelism(2);

        //2、读取数据
        //nc -lk 8888
        DataStream<String> linesDS = env.socketTextStream("master", 8888);
        System.out.println(linesDS.getParallelism());

        //3、转换成kv
        DataStream<Tuple2<String, Integer>> kvDS = linesDS
                .map(word -> Tuple2.of(word, 1), Types.TUPLE(Types.STRING, Types.INT));

        //4、统计单词的数量
        KeyedStream<Tuple2<String, Integer>, String> KeyByDS = kvDS.keyBy(kv -> kv.f0);


        DataStream<Tuple2<String, Integer>> countDS = KeyByDS.sum(1);

        //大于结果
        countDS.print();

        //启动
        env.execute();
    }
}
